package com.dwx.guli.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dwx.guli.service.base.result.R;
import com.dwx.guli.service.edu.entity.Teacher;
import com.dwx.guli.service.edu.entity.query.TeacherQuery;
import com.dwx.guli.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author dwx
 * @since 2021-06-30
 */
@Api(tags = "讲师管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @ApiOperation("所有讲师列表")
    @GetMapping("/list")
    public R list(){
        List<Map<String, Object>> list = teacherService.nameList();
        if(null!=list&&0<list.size()){
            return R.ok().data("items",list).message("获取讲师列表成功");
        }else {
            return R.error().message("获取讲师列表失败");
        }
    }
    @ApiOperation(value = "根据ID删除讲师", notes = "根据ID删除讲师")
    @DeleteMapping("/delete/{id}")
    public R delete(
            @ApiParam(value = "讲师ID",required = true)
            @PathVariable String id){
        boolean b = teacherService.removeAvatarById(id);
        if(!b){
            log.warn("讲师头像删除失败，讲师id： " + id);
        }

        boolean result=teacherService.removeById(id);
        if(result){
            log.info("根据"+id+"删除讲师成功");
            return R.ok().message("删除成功");
        }else{
            log.error("根据"+id+"删除讲师失败");
            return R.error().message("删除失败");
        }
    }
    @ApiOperation("新增讲师")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "新增数据",required = true)
            @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result){
            log.info("新增"+teacher+"讲师成功");
            return R.ok().message("添加成功");
        }else {
            log.error("新增"+teacher+"讲师失败");
            return R.error().message("添加失败");
        }
    }
    @ApiOperation("分页查询")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页数量",required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询条件")
            TeacherQuery teacherQuery){
        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);
        log.info("分页查询,条件"+teacherQuery);
        return R.ok().message("查询成功").data("pageModel",pageModel);
    }
    @ApiOperation("修改讲师")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "修改条件",required = true)
            @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if (result){
            log.info("修改"+teacher+"讲师成功");
            return R.ok().message("修改成功");
        }else {
            log.error("修改"+teacher+"讲师失败");
            return R.error().message("修改失败");
        }
    }
    @ApiOperation("根据ID查询")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "讲师ID",required = true)
            @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if (null!=teacher){
            log.info("根据"+id+"查询讲师成功");
            return R.ok().message("查询成功").data("item",teacher);
        }else {
            log.error("根据"+id+"查询讲师失败");
            return R.error().message("数据不存在");
        }
    }
    @ApiOperation("根据ID批量删除")
    @DeleteMapping("/batch-remove")
    public R batchByIds(
            @ApiParam(value = "讲师ID数组",required = true)
            @RequestBody List<String> ids){
        boolean result = teacherService.removeByIds(ids);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
    @ApiOperation("根据左关键字查询讲师名列表")
    @GetMapping("/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "左关键字",required = true)
            @PathVariable String key){
        List<Map<String ,Object>> list= teacherService.selectNameListByKey(key);
        return R.ok().data("nameList",list);
    }

}

